#include "StdAfx.h"
#include "ddocument.h"
#include "dclassentity.h"
#include "dentityfactory.h"

DDocument::DDocument(QObject *parent)
	: QObject(parent)
	, m_nMaxClassLevel(-1)
{

}

DDocument::~DDocument()
{

}

void DDocument::addClass(DClassEntity* pClass, DClassEntity* pParent)
{
	if(pParent)
	{
		pParent->addSubclass(pClass);
	}
	m_classes.append(pClass);
	updateMaxClassLevel();
	emit classAdded(pClass);
}

DClassEntity* DDocument::addClass(ClassType type, const QString& name, Access inheritanceType, DClassEntity* pParent)
{
	DEntityFactory factory;
	DClassEntity* pClass = factory.createClassEntity(type, name, inheritanceType);
	addClass(pClass, pParent);
	return pClass;
}

void DDocument::removeClass(DClassEntity* pClass)
{
	if(DClassEntity* pBaseClass = pClass->baseClass())
	{
		pBaseClass->removeSubclass(pClass);
	}

	m_classes.removeOne(pClass);
	updateMaxClassLevel();
	emit classRemoved(pClass);
}

const QList<DClassEntity*>& DDocument::classes() const
{
	return m_classes;
}

void DDocument::updateMaxClassLevel()
{
	m_nMaxClassLevel = -1;
	foreach(DClassEntity* pClass, m_classes)
	{
		int classLevel = pClass->level();
		if( classLevel > m_nMaxClassLevel)
		{
			m_nMaxClassLevel = classLevel;
		}
	}
}

int	DDocument::maxClassLevel() const
{
	return m_nMaxClassLevel;
}